package com.tenglong.teacher.ui.dialog;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tenglong.teacher.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class DialogDXFragment extends DialogFragment implements View.OnClickListener {

    private static final String KEY_DETAILS = "content";
    private static final String KEY_TITLE = "title";
    private static final String KEY_CANCLE = "cancle";
    private static final String KEY_CANFIRM = "confirm";

    @SuppressLint("ValidFragment")
    private DialogDXFragment() {
        // Required empty public constructor
    }


    public static DialogDXFragment newInstance(String content, String title, String cancle, String confirm) {
        DialogDXFragment fragment = new DialogDXFragment();
        Bundle args = new Bundle();
        args.putString(KEY_DETAILS, content);
        args.putString(KEY_TITLE, title);
        args.putString(KEY_CANCLE, cancle);
        args.putString(KEY_CANFIRM, confirm);
        fragment.setArguments(args);
        return fragment;
    }

    public static DialogDXFragment newInstance(String content, String title) {
        DialogDXFragment fragment = new DialogDXFragment();
        Bundle args = new Bundle();
        args.putString(KEY_DETAILS, content);
        args.putString(KEY_TITLE, title);
        fragment.setArguments(args);
        return fragment;
    }

    public static DialogDXFragment newInstance(String content, String cancle, String confirm) {
        DialogDXFragment fragment = new DialogDXFragment();
        Bundle args = new Bundle();
        args.putString(KEY_DETAILS, content);
        args.putString(KEY_CANCLE, cancle);
        args.putString(KEY_CANFIRM, confirm);
        fragment.setArguments(args);
        return fragment;
    }

    public static DialogDXFragment newInstance(String content) {
        DialogDXFragment fragment = new DialogDXFragment();
        Bundle args = new Bundle();
        args.putString(KEY_DETAILS, content);
        fragment.setArguments(args);
        return fragment;
    }


    public DialogDXFragment setShowNewsDialogData(String content, String title, String cancle, String confirm) {
        this.mDetails = content;
        this.title = title;
        this.cancle = cancle;
        this.confirm = confirm;
        setContent();
        return this;
    }

    public DialogDXFragment setShowNewsDialogData(String content, String title) {
        this.mDetails = content;
        this.title = title;
        this.cancle = null;
        this.confirm = null;
        setContent();
        return this;
    }

    public DialogDXFragment setShowNewsDialogData(String content, String cancle, String confirm) {
        this.mDetails = content;
        this.title = null;
        this.cancle = cancle;
        this.confirm = confirm;
        setContent();
        return this;
    }


    private TextView mContent;
    private TextView mCancle;
    private TextView mToFind;
    private TextView mTitle;

    private String mDetails;
    private String title;
    private String cancle;
    private String confirm;

    private ShowNewsDialogListener listener;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (getArguments() != null) {
            mDetails = getArguments().getString(KEY_DETAILS);
            title = getArguments().getString(KEY_TITLE);
            cancle = getArguments().getString(KEY_CANCLE);
            confirm = getArguments().getString(KEY_CANFIRM);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dialog_dx, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setListeners();
        setContent();
    }

    private void setListeners() {
        mCancle.setOnClickListener(this);
        mToFind.setOnClickListener(this);
    }

    private void initView(View view) {
        mContent = view.findViewById(R.id.content);
        mCancle = view.findViewById(R.id.cancle);
        mToFind = view.findViewById(R.id.toFind);
        mTitle = view.findViewById(R.id.title);
    }


    private void setContent() {
        mContent.setText(mDetails);
        if (title != null) {
            mTitle.setText(title);
        } else {
            mTitle.setText(R.string.dialog_text_reminder);
        }
        if (cancle != null) {
            mCancle.setText(cancle);
        } else {
            mCancle.setText(R.string.dialog_btn_cancel);
        }
        if (confirm != null) {
            mToFind.setText(confirm);
        } else {
            mToFind.setText(R.string.dialog_btn_confirm);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toFind:
                if (listener != null) {
                    listener.onShowNewsDialogListener();
                }
            case R.id.cancle:
                dismiss();
                break;
        }
    }

    public DialogDXFragment setShowNewsDialogListener(ShowNewsDialogListener listener) {
        this.listener = listener;
        return this;
    }

    public interface ShowNewsDialogListener {
        void onShowNewsDialogListener();
    }
}
